<!--
 * @Description: 编辑绑定第三方店铺弹窗
 * @Author: liyujie
 * @Date: 2021/10/12
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/12
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="编辑绑定店铺"
            :visible.sync="dialogVisible"
            width="740px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-form
                    label-width="126px"
                    @submit.native.prevent
                >
                    <el-form-item label="掌大店铺名称">
                        <el-input
                            v-model="data.shop_title"
                            size="medium"
                            clearable
                        />
                    </el-form-item>
                    <el-form-item label="绑定公司">
                        {{ data.company_name ? data.company_name : '--' }}
                    </el-form-item>
                    <el-form-item
                        label="关联的淘宝店铺"
                        v-if="taoShop.length"
                    >
                        <div
                            class="shop-row"
                            v-for="(row, index) in taoShop"
                            :key="row.social_shop_no"
                        >
                            <div class="left">
                                <div class="shop-row-item">店铺名称：{{ row.title }}</div>
                                <div class="shop-row-item last">店铺ID：{{ row.social_shop_no }}</div>
                            </div>
                            <el-button
                                size="medium"
                                type="text"
                                style="text-decoration: underline"
                                @click="unboundShop(row, index, 'taoShop')"
                            >
                                解绑
                            </el-button>
                        </div>
                    </el-form-item>
                    <el-form-item
                        label="关联的京东店铺"
                        v-if="jdShop.length"
                    >
                        <div
                            class="shop-row"
                            v-for="(row, index) in jdShop"
                            :key="row.social_shop_no"
                        >
                            <div class="left">
                                <div class="shop-row-item">店铺名称：{{ row.title }}</div>
                                <div class="shop-row-item last">店铺ID：{{ row.social_shop_no }}</div>
                            </div>
                            <el-button
                                size="medium"
                                type="text"
                                style="text-decoration: underline"
                                @click="unboundShop(row, index, 'jdShop')"
                            >
                                解绑
                            </el-button>
                        </div>
                    </el-form-item>
                    <el-form-item
                        label="关联的拼多多店铺"
                        v-if="pddShop.length"
                    >
                        <div
                            class="shop-row"
                            v-for="(row, index) in pddShop"
                            :key="row.social_shop_no"
                        >
                            <div class="left">
                                <div class="shop-row-item">店铺名称：{{ row.title }}</div>
                                <div class="shop-row-item last">店铺ID：{{ row.social_shop_no }}</div>
                            </div>
                            <el-button
                                size="medium"
                                type="text"
                                style="text-decoration: underline"
                                @click="unboundShop(row, index, 'pddShop')"
                            >
                                解绑
                            </el-button>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px;margin-left: 32px"
                    @click="handleConfirm()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>

import {deepClone} from "@/assets/js/utils";
import Vue from "vue";

const Dialog = {
    name: "EditBindingShop",
    props: {
        rowData: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            taoShop: [],
            jdShop: [],
            pddShop: []
        };
    },
    computed: {
        data() {
            return this.rowData;
        }
    },
    watch: {
        rowData: {
            handler(value) {
                this.taoShop = value.social_shop_list.filter(item => {
                    return item.shop_social_type === 1;
                });
                this.jdShop = value.social_shop_list.filter(item => {
                    return item.shop_social_type === 2;
                });
                this.pddShop = value.social_shop_list.filter(item => {
                    return item.shop_social_type === 3;
                });
            },
            deep: true,
            immediate: true
        }
    },
    mounted() {},
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 解绑店铺
         */
        unboundShop(row, index, type) {
            let params = {
                shop_social_type: row.shop_social_type,
                social_shop_no: row.social_shop_no,
                shop_no: ''
            };

            this.$post('/student/shop%5Cbind_social_shop', params, resp => {
                if (resp.code === 1) {
                    //解绑成功删除选项
                    this[type].splice(index, 1);
                    this.$notify({
                        title: '提示',
                        message: '解绑成功',
                        duration: 2000,
                        type: 'success'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 点击确定
         */
        handleConfirm() {
            this.$post('/student/shop%5Cmodify', {
                shop_no: this.data.shop_no,
                shop_title: this.data.shop_title
            }, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.$emit('confirm', true);
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
    }
};

export default Dialog;

const editBindingShopDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { editBindingShopDialog };
</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
        .shop-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            &-item {
                &.last {
                    margin-left: 24px;
                }
            }
            .left {
                display: flex;
            }
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
}

/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;

    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }

    .el-icon-close:before {
        color: rgb(194, 194, 194);
        font-size: 17px;
        font-weight: bold;
    }

    .el-dialog__headerbtn {
        top: 14px;
    }
}

/deep/ .el-dialog__body {
    padding: 24px 32px;
}
</style>
